package com.simple.editor.action.impl;

import com.simple.editor.ApplicationContext;
import com.simple.editor.action.EditorException;
import com.simple.editor.converter.FileModelConverter;
import com.simple.editor.ui.FileModel;
import com.simple.editor.ui.FileTreePane;
import com.simple.editor.ui.InfoPane;
import com.simple.editor.vcs.VcsClientRegistry;

/**
 * {@link OpenProjectEditorUIAction} handles user "Open Project" action.
 * <p/>
 * Created with IntelliJ IDEA.
 * User: vlad
 * Date: 6/8/13
 * Time: 11:29 AM
 * To change this template use File | Settings | File Templates.
 */
public class OpenProjectEditorUIAction extends AbstracEditorUIAction {

    private final ApplicationContext ctx;

    public OpenProjectEditorUIAction(final ApplicationContext ctx) {
        this.ctx = ctx;
    }

    @Override
    public void perform(final FileModel... files) {
        if (files != null && files.length == 1) {
            final FileModel file = files[0];
            final InfoPane infoPane = ctx.getComponent(InfoPane.class);
            infoPane.addInfo("Opening project...");
            try {
                ctx.getComponent(VcsClientRegistry.class).setVcsClientForThePath(FileModelConverter.convert(file));
                ctx.getComponent(FileTreePane.class).open(file);
                infoPane.addInfo("Project opened.");
            } catch (EditorException e) {
                infoPane.addError(String.format("Fail to open project. %s", e.getMessage()));
            }
        }
    }
}

